package com.ddstation.drug.service.impl;

import com.ddstation.drug.entity.MaInvsBaseDrug;
import com.ddstation.drug.mapper.MaInvsBaseDrugMapper;
import com.ddstation.drug.service.MaInvsDrugService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;

/**
 * @Description :
 * @Author : Fubb
 * @Date :Created 21:06 2020/2/29
 * @Modified by :
 **/
@Service
public class IMaInvsDrugServiceImpl implements MaInvsDrugService {

    @Inject
    private MaInvsBaseDrugMapper maInvsBaseDrugMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void selectBySpecification() throws Exception {

        List<MaInvsBaseDrug> maInvsBaseDrugs = new ArrayList<>();
        List<MaInvsBaseDrug> maInvsBaseDrugList = maInvsBaseDrugMapper.selectByMaohao();
        Pattern pattern = compile("^-?\\d+(\\.\\d+)?$");
        Pattern pattern1 = compile(".*[a-zA-z].*");
        Pattern pattern2 = compile(".*[\\u4e00-\\u9fa5].*");
        Pattern pattern4 = compile("[0-9]*");

//        String regEx = "[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）()——+|{}【】‘；：”“’。，、？]|\n|\r|\t";
        for (int j = 0; j < maInvsBaseDrugList.size(); j++) {
            MaInvsBaseDrug maInvsBaseDrug = maInvsBaseDrugList.get(j);
            System.out.println("==============" + maInvsBaseDrug.getId() + ":" + maInvsBaseDrug.getSpecification());
            MaInvsBaseDrug entity = new MaInvsBaseDrug();
            //去空格
            String spec = maInvsBaseDrug.getSpecification().replaceAll(" ", "");
            String[] specSplit;
            if (spec.contains("?") || spec.contains("？"))
                continue;
            {
            }
            if (spec.contains("*") || spec.contains("(") || spec.contains("（") || spec.contains("%")|| spec.contains("％") || spec.contains("/") || spec.contains(";") || spec.contains("；") || spec.contains("+") || spec.contains("×") || spec.contains("，") || spec.contains(",")) {
                specSplit = spec.split("\\*|\\(|（|/|%|；|;|\\+|×|，|,|％");
            } else {
                Matcher isChiness = pattern2.matcher(spec);
                if (!isChiness.matches()) {
                    specSplit = new String[1];
                    specSplit[0] = spec;
                } else {
                    Matcher isNumFirst = pattern.matcher(spec.charAt(0) + "");
                    if(isNumFirst.matches()){
                        specSplit = new String[1];
                        specSplit[0] = spec;
                    }else{
                        specSplit = new String[1];
                    }
//                    specSplit[0] = "";
                }
            }
            List<String> specSplitList = new ArrayList<>(Arrays.asList(specSplit));
            List<String> specSplitList1 = new ArrayList<>();
            String contentSpec = "";
            String contentSpecUnit = "";
            String preparationAmount = "";
            String preparationUnit = "";
            String stockkeepingUnit = "";
            if (specSplitList.size() > 0) {
                for (String index : specSplitList) {
                    //判断字符串是否为空
                    if (Objects.nonNull(index)) {
                        Matcher isAllNum = pattern1.matcher(index);
                        Matcher isAllNum1 = pattern2.matcher(index);
                        if (!isAllNum.matches() && !isAllNum1.matches()) {
//                        specSplitList.remove(index);
                            continue;
                        }
                        if (index.contains(")") || index.contains("）")) {
                            continue;
                        }
                        if (index.contains(":") || index.contains("：") || index.contains("∶")) {

                            String specSplitSplit = "";
                            if (index.contains(":")) {
                                if (index.split(":").length > 1) {
                                    specSplitSplit = index.split(":")[1];
                                } else {
                                    specSplitSplit = index.split(":")[0];
                                }
                            } else if (index.contains("：")) {
                                if (index.split("：").length > 1) {
                                    specSplitSplit = index.split("：")[1];
                                } else {
                                    specSplitSplit = index.split("：")[0];
                                }
                            }else{
                                if (index.split("∶").length > 1) {
                                    specSplitSplit = index.split("∶")[1];
                                } else {
                                    specSplitSplit = index.split("∶")[0];
                                }
                            }
                            Matcher isNum = pattern.matcher(specSplitSplit.charAt(0) + "");
                            if (Objects.isNull(entity.getContentSpec())) {
                                if (isNum.matches()) {
                                    Splits splits = new Splits(specSplitSplit);
                                    contentSpec = splits.getNumStr();
                                    contentSpecUnit = splits.getUnitStr();
                                    BigDecimal contentSpecDecimal = new BigDecimal(contentSpec);
                                    if (StringUtils.isNotBlank(contentSpec)) {
                                        entity.setId(maInvsBaseDrug.getId());
                                        entity.setContentSpec(contentSpecDecimal);
                                        entity.setContentUnitText(contentSpecUnit);
                                    }
                                    continue;

                                } else {
                                    continue;
                                }
                            }
                        }
                        Matcher isNum1 = pattern.matcher(index.charAt(0) + "");
                        Matcher isChiness1 = pattern2.matcher(index);
                        if (isNum1.matches()) {
                            specSplitList1.add(index);
                        }
                    }
                }
                if (specSplitList1.size() > 0) {
                    //如果剂量或者剂量单位不为空，就将后面的两个值作为最小包装单位根最小包装量。
                    //如果剂量或者剂量单位为空，就将后面的值分别赋值给剂量，剂量单位，最小包装量最小包装单位。
                    if (contentSpec != "" || contentSpecUnit != "") {
                        for (int i = 0; i < specSplitList1.size(); i++) {
                            if (i == 0) {
                                Splits splits = new Splits(specSplitList1.get(i));
                                preparationAmount = splits.getNumStr();
                                preparationUnit = splits.getUnitStr();
                                if (!preparationAmount.contains(".")) {
                                    if (StringUtils.isBlank(preparationAmount)) {
                                        entity.setPreparationAmount(Short.valueOf("1"));
                                    } else {
                                        entity.setPreparationAmount(Short.valueOf(preparationAmount));
                                    }
                                }
                                entity.setPreparationUnitText(preparationUnit);
                            }
                            if (i == 1) {
                                Splits splits = new Splits(specSplitList1.get(i));
                                stockkeepingUnit = splits.getUnitStr();
                                entity.setStockKeepingUnitText(stockkeepingUnit);
                            }
                        }
                    }
                    if (contentSpec == "" && contentSpecUnit == "") {
                        for (int k = 0; k < specSplitList1.size(); k++) {
                            if (k == 0) {
                                Splits splits = new Splits(specSplitList1.get(k));
                                contentSpec = splits.getNumStr();
                                contentSpecUnit = splits.getUnitStr();
                                if (StringUtils.isNotBlank(contentSpec)) {
                                    BigDecimal contentSpecDecimal = new BigDecimal(contentSpec);
                                    entity.setId(maInvsBaseDrug.getId());
                                    entity.setContentSpec(contentSpecDecimal);
                                    entity.setContentUnitText(contentSpecUnit);
                                }
                            }
                            if (k == 1) {
                                Splits splits = new Splits(specSplitList1.get(k));
                                preparationAmount = splits.getNumStr();
                                preparationUnit = splits.getUnitStr();
                                if (!preparationAmount.contains(".")) {
                                    if (preparationAmount == "" || preparationAmount == null) {
                                        entity.setPreparationAmount(Short.valueOf("1"));
                                    } else {
                                        entity.setPreparationAmount(Short.valueOf(preparationAmount));
                                    }
                                    entity.setPreparationUnitText(preparationUnit);
                                }
                            }
                            if (k == 2) {
                                Splits splits = new Splits(specSplitList1.get(k));
                                stockkeepingUnit = splits.getUnitStr();
                                if (stockkeepingUnit.equals("盒") || stockkeepingUnit.equals("瓶") || stockkeepingUnit.equals("袋") || stockkeepingUnit.equals("包")
                                        || stockkeepingUnit.equals("片") || stockkeepingUnit.equals("粒") || stockkeepingUnit.equals("罐") || stockkeepingUnit.equals("板") || stockkeepingUnit.equals("大盒")
                                        || stockkeepingUnit.equals("贴") || stockkeepingUnit.equals("个")) {
                                    entity.setStockKeepingUnitText(stockkeepingUnit);
                                }
                            }
                        }
                    }

                }
                if (Objects.nonNull(entity.getId())) {
                    maInvsBaseDrugs.add(entity);
                }
            }

        }
        if (maInvsBaseDrugs.size() > 0) {
            maInvsBaseDrugMapper.update(maInvsBaseDrugs);
        }
    }
}


